using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;

namespace WarehouseManagement.UI
{
    public partial class NhomVatTu : Form
    {
        ConnectManager cm = new ConnectManager();
        Validator vali = new Validator();
        DataTable tbVT1, tbVT2, tbVT3, tbVT45, tbVT67, tbVT89;
        int treeViewLevel = 0;
        
        public NhomVatTu()
        {
            InitializeComponent();
            loadTreeView();
        }

        private void loadTreeView()
        {
            cm.conn.Open();
            tbVT1 = cm.getTable("Select * from NhomVT_1");
            TreeNode tnLevel1;
            for (int i = 0 ; i < tbVT1.Rows.Count ; i++) // loop level 1
            {
                tbVT2 = cm.getTable("Select * from NhomVT_2 where ID1 = '" +tbVT1.Rows[i]["ID1"].ToString()+ "'");
                if (tbVT2.Rows.Count > 0)
                {
                    TreeNode[] tnLevel2 = new TreeNode[tbVT2.Rows.Count];
                    for (int i2 = 0; i2 < tbVT2.Rows.Count; i2++) // Loop cho level 2
                    {
                        string cmd3 = "Select * from NhomVT_3 where ID12 = '"+tbVT2.Rows[i2]["ID12"].ToString()+"'";
                        tbVT3 = cm.getTable(cmd3);

                        if (tbVT3.Rows.Count > 0)
                        {
                            TreeNode[] tnLevel3 = new TreeNode[tbVT3.Rows.Count];
                            for (int i3 = 0; i3 <tbVT3.Rows.Count ; i3++) // loop Level 3
                            {
                                string cmd45 = "Select * from NhomVT_45 where ID23 = '"+tbVT3.Rows[i3]["ID23"].ToString()+"'";
                                tbVT45 = cm.getTable(cmd45);

                                if (tbVT45.Rows.Count > 0)
                                {
                                    TreeNode[] tnLevel45 = new TreeNode[tbVT45.Rows.Count];
                                    for (int i4 = 0; i4 < tbVT45.Rows.Count; i4++) // Loop Level 45
                                    {
                                        string cmd67 = "select * from NhomVT_67 where ID45 = '"+tbVT45.Rows[i4]["ID45"].ToString()+"'";
                                        tbVT67 = cm.getTable(cmd67);

                                        if (tbVT67.Rows.Count > 0)
                                        {
                                            TreeNode[] tnLevel67 = new TreeNode[tbVT67.Rows.Count];
                                            for (int i6 = 0; i6 < tbVT67.Rows.Count; i6++)
                                            {
                                                string cmd89 = "Select * from NhomVT_89 where ID67 = '" + tbVT67.Rows[i6]["ID67"] + "'";
                                                tbVT89 = cm.getTable(cmd89);

                                                if (tbVT89.Rows.Count > 0)
                                                {
                                                    TreeNode[] tnLevel8 = new TreeNode[tbVT89.Rows.Count];

                                                    for (int i8 = 0; i8 < tbVT89.Rows.Count; i8++)
                                                    {
                                                        tnLevel8[i8] = new TreeNode(tbVT89.Rows[i8]["TenNhom"].ToString());
                                                        tnLevel8[i8].Name = tbVT89.Rows[i8]["ID89"].ToString();
                                                    }
                                                    tnLevel67[i6] = new TreeNode(tbVT67.Rows[i6]["TenNhom"].ToString(), tnLevel8);

                                                }
                                                else
                                                {
                                                    tnLevel67[i6] = new TreeNode(tbVT67.Rows[i6]["TenNhom"].ToString());   
                                                }
                                                tnLevel67[i6].Name = tbVT67.Rows[i6]["ID67"].ToString();
                                            }
                                            tnLevel45[i4] = new TreeNode(tbVT45.Rows[i4]["TenNhom"].ToString(), tnLevel67);
                                        }
                                        else
                                        {
                                            tnLevel45[i4] = new TreeNode(tbVT45.Rows[i4]["TenNhom"].ToString());
                                        }
                                        tnLevel45[i4].Name = tbVT45.Rows[i4]["ID45"].ToString();
                                    }
                                    tnLevel3[i3] = new TreeNode(tbVT3.Rows[i3]["TenNhom"].ToString(), tnLevel45);
                                }
                                else
                                {
                                    tnLevel3[i3] = new TreeNode(tbVT3.Rows[i3]["TenNhom"].ToString());
                                }
                                tnLevel3[i3].Name = tbVT3.Rows[i3]["ID23"].ToString();

                            }
                            tnLevel2[i2] = new TreeNode(tbVT2.Rows[i2]["TenNhom"].ToString(),tnLevel3);
                        }
                        else
                        {
                            tnLevel2[i2] = new TreeNode(tbVT2.Rows[i2]["TenNhom"].ToString());
                        }
                        tnLevel2[i2].Name = tbVT2.Rows[i2]["ID12"].ToString();
                    }
                    tnLevel1 = new TreeNode(tbVT1.Rows[i]["TenNhom"].ToString(),tnLevel2);
                    tnLevel1.Name = tbVT1.Rows[i]["ID1"].ToString();
                    treeView1.Nodes.Add(tnLevel1);
                }
                else
                {
                    tnLevel1 = new TreeNode(tbVT1.Rows[i]["TenNhom"].ToString());
                    tnLevel1.Name = tbVT1.Rows[i]["ID1"].ToString();
                    treeView1.Nodes.Add(tnLevel1);
                }

            }
            cm.conn.Close();
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            txtIDCha.Text = e.Node.Name.ToString();
            txtTenNhomCha.Text = e.Node.Text;

            txt1.Mask = "000.000";
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (treeViewLevel == 0)
                {
                        string cmd;
                        SqlCommand sqlInsert;
                        cm.conn.Open();
                        cmd = "Insert into NhomVT_1 (ID1, TenNhom) Values ('" + txtIDCha.Text + "','" + txtTenNhomCha.Text + "')";
                        sqlInsert = new SqlCommand(cmd, cm.conn);
                        sqlInsert.ExecuteNonQuery();
                        cm.conn.Close();
                    
                }
                else if (treeViewLevel == 1)
                {
                    cm.conn.Open();
                    InsertData("NhomVT_2", "ID12", "ID1", txtIDCon.Text, txtIDCha.Text, txttenNhomCon.Text);
                    cm.conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                cm.conn.Close();
            }
            treeView1.Nodes.Clear();
            loadTreeView();
        }

        private void InsertData(string tbName, string IDxx, string IDx, string ParaID, string paraIDx, string sTenNhom)
        {
            SqlCommand sqlInsert;
            string sIDxx = paraIDx + ParaID;
            string cmd = "Insert into "+tbName+" (" + IDxx + ", ID, " + IDx + ", TenNhom) Values ('" + sIDxx + "', '" + ParaID + "', '" + paraIDx + "', '" + sTenNhom + "')";
            sqlInsert = new SqlCommand(cmd, cm.conn);
            sqlInsert.ExecuteNonQuery();
        }


    }
}